<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    function compileCode(src){
      src =`with (sandbox){${src}}`
      const code =  new Function('sandbox',src)
      return function (sandbox){
        const sandboxProxy = new Proxy(sandbox,{has,get})
        return code.call(sandboxProxy,sandboxProxy)
      }
    }
    function has(target,key) {
      return true
    }
    function get(target,key) {
      if(key === Symbol.unscopables) return undefined
      return target[key]
    }

    let obj = {a:1}
    compileCode('a=2')(obj)
    console.log(obj);
  </script>
</body>
</html>